/************************************************************
*          	   RoyalTek Company Limited					    *
*      	       Copyright 2003 RoyalTek Co., Ltd.			*
*     	       All Rights Reserved						    *
*************************************************************
 Description:
	Com port Moudle
     	
 Revision History:
	2003/01/28 Jordan Yu, version 1.0
	2003/04/14
		Update function Prolog
 		
 Notes:
  
 ***********************************************************/
#include "stdafx.h"
#include "CDBG.h"
#include "resource.h"



/************************************************************
Name:
	CDBG :: CDBG()

Defined in:
	CDBG.h

Parameters:
		
Return:
			
Description:
	Constructor function
******************************************************************/
CDBG :: CDBG()
{
	m_hLogFile	=	INVALID_HANDLE_VALUE;
}


/************************************************************
Name:
	int CDBG :: OpenLogFile ( TCHAR *szLogPathName, bool bIfOverWrite )

Defined in:
	CDBG.h

Parameters:
	[in] szLogPathName	:	Log file name (including path)
	[in] bIfOverWrite	:	Overwite the file or not

Return:
	CDBG_OK	:	success
	CDBG_OPEN_FILE_FAIL: 
	CDBG_FAIL	:	fail

Description:
	System may put read/write data into log file for debugging
	This function is "open log file".
******************************************************************/
int CDBG :: OpenLogFile ( TCHAR *szLogPathName, bool bIfOverWrite )
{
#ifndef ENABLE_DBGOUT
	return CDBG_OK;
#endif

   if (szLogPathName == NULL)
	   return CDBG_FAIL;

   CloseLogFile ();

   m_hLogFile = CreateFile  ( szLogPathName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );  
   if ( m_hLogFile == INVALID_HANDLE_VALUE )
      return CDBG_OPEN_FILE_FAIL;

   if ( bIfOverWrite )
      ::SetEndOfFile ( m_hLogFile );
   else
      ::SetFilePointer ( m_hLogFile, 0, NULL, FILE_END );

   return CDBG_OK;
}

/************************************************************
Name:
	int CDBG :: CloseLogFile ()

Defined in:
	CDBG.h

Parameters:

Return:
	CDBG_OK		:	success
	CDBG_FAIL	:	fail

Description:
	System will put read/write data into log file for debugging
	This function is "close log file".
******************************************************************/
int CDBG :: CloseLogFile ()
{
#ifndef ENABLE_DBGOUT
	return CDBG_OK;
#endif
   if ( m_hLogFile == INVALID_HANDLE_VALUE )
		return CDBG_OK;

   if(CloseHandle ( m_hLogFile ) == 0)
		return CDBG_FAIL;
   else {
		m_hLogFile = INVALID_HANDLE_VALUE;
		return CDBG_OK;
   }
}

/************************************************************
Name:
	int CDBG :: WriteToLog(BYTE* bWriteString , DWORD nRequestLen);

Defined in:
	CDBG.h

Parameters:
	[in] szWriteString	: written data
	[in] nRequestLen	: number of bytes. 	0 means it is ascii string.

Return:
	CDBG_OK				:	success
	CDBG_INVALID_HANDLE	:	file handle is invalid
	CDBG_FAIL			:	fail

Description:
	System will put read/write data into log file for debugging
	This function is "close log file".
******************************************************************/
int CDBG :: WriteToLog(BYTE* bWriteString , DWORD nRequestLen)
{

#ifdef ENABLE_DBGOUT
	int iRtn = CDBG_FAIL;
	DWORD uWrited;
	DWORD dwWriteLen;
	TCHAR szTmpStr[512];
	CString szMsg;

	if (bWriteString == NULL)
		return CDBG_FAIL;

	if ( m_hLogFile == INVALID_HANDLE_VALUE )
		return CDBG_INVALID_HANDLE;

	if (nRequestLen == 0) // it is ascii string
	{
		dwWriteLen = strlen( (char*) bWriteString );
	} 
	else // it is binary data
	{
		dwWriteLen = nRequestLen;
	}
	if (dwWriteLen > MAX_LOG_STRING)
		return CDBG_FAIL;

	iRtn = WriteFile (m_hLogFile, bWriteString, dwWriteLen, &uWrited, NULL );
	if (iRtn == 0)
	{		
		szMsg.LoadString(IDS_WriteFileError);
		GENsprintf(szTmpStr,szMsg,GetLastError());
		AfxMessageBox(szTmpStr);
		return CDBG_FAIL;
	}
#endif
	return CDBG_OK;
}



/************************************************************
Name:
	int CDBG::DBGOut(char* format,[, argument]... )

Defined in:
	CDBG.h

Parameters:
	[in] format		: Format control 
	[out] argument	: Optional arguments 

Return:
	CDBG_OK		:	success		
	CDBG_FAIL	:	fail

Description:
	Output debug message
******************************************************************/
int CDBG :: DBGOut(char* format, ...)
{
	int		iRtn = CDBG_OK;

#ifdef ENABLE_DBGOUT
	char		szMsg[256], szDBGMsg[256];
	va_list		marker;

	va_start(marker, format);
	vsprintf(szMsg, format, marker);
	sprintf(szDBGMsg,"%s\n",szMsg);

#ifdef DBG_OUT_TO_FILE
	iRtn = WriteToLog ((BYTE*) szDBGMsg);
	iRtn = WriteToLog ((BYTE*) _T("\r\n"));
#else
#ifdef _WIN32_WCE
	TCHAR wszDBGMsg[128];	
	int iWlen;
	iWlen= MultiByteToWideChar(CP_ACP,0,szDBGMsg,-1,NULL,0);
	MultiByteToWideChar(CP_ACP,0,szDBGMsg,-1,wszDBGMsg,iWlen);
	AfxMessageBox(wszDBGMsg);
#else
	AfxMessageBox(szDBGMsg);
#endif
#endif

#endif
	return iRtn;
}


